﻿<template>
  <a-drawer
    :title="title"
    width="90%"
    :visible="visible"
    @close="
      () => {
        this.visible = false
      }
    "
  >
    <a-form-model ref="form" :model="entity" :rules="rules" v-bind="layout">
      <a-row>
        <a-col :span="8">
          <a-form-model-item label="计量单位组编码" prop="cGroupCode">
            <a-input v-model="entity.cGroupCode" autocomplete="off" placeholder="计量单位组编码" />
          </a-form-model-item>
        </a-col>

        <a-col :span="8">
          <a-form-model-item label="计量单位组名称" prop="cGroupName">
            <a-input v-model="entity.cGroupName" autocomplete="off" placeholder="计量单位组名称" />
          </a-form-model-item>
        </a-col>

        <a-col :span="8">
          <a-form-model-item label="计量单位组类别" prop="iGroupType">
            <a-input v-model="entity.iGroupType" autocomplete="off" placeholder="计量单位组类别" />
          </a-form-model-item>
        </a-col>
      </a-row>
      <a-row>
        <!-- <a-col :span="8">
          <a-form-model-item label="时间戳" prop="pubufts">
            <a-input v-model="entity.pubufts" autocomplete="off" placeholder="时间戳" />
          </a-form-model-item>
        </a-col> -->

        <a-col :span="8">
          <a-form-model-item label="对应存货编码" prop="cGrpRelInvCode">
            <a-input v-model="entity.cGrpRelInvCode" autocomplete="off" placeholder="对应存货编码" />
          </a-form-model-item>
        </a-col>

        <!-- <a-col :span="8">
          <a-form-model-item label="是否默认组" prop="bDefaultGroup">
            <a-input v-model="entity.bDefaultGroup" autocomplete="off" placeholder="是否默认组" />
          </a-form-model-item>
        </a-col> -->
      </a-row>
    </a-form-model>
    <a-table
      ref="table"
      :scroll="{ y: 530 }"
      :columns="columns"
      :rowKey="(row, index) => row.index"
      :dataSource="data"
      :pagination="pagination"
      :loading="loading"
      @change="handleTableChange"
      :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
      :bordered="true"
      size="small"
    >
    </a-table>
    <div
      :style="{
        position: 'absolute',
        right: 0,
        bottom: 0,
        width: '100%',
        borderTop: '1px solid #e9e9e9',
        padding: '4px 16px',
        background: '#fff',
        textAlign: 'right',
        zIndex: 1
      }"
    >
      <a-button @click="close">取消</a-button>&nbsp;
    </div>
  </a-drawer>
</template>

<script>
const columns = [
  { title: '计量单位编码', dataIndex: 'cComunitCode' },
  { title: '计量单位名称', dataIndex: 'cComUnitName' },
  { title: '计量单位组编码', dataIndex: 'cGroupCode' },
  { title: '对应条形码编码', dataIndex: 'cBarCode' },
  { title: '是否主计量单位', dataIndex: 'bMainUnit' },
  { title: '换算率', dataIndex: 'iChangRate' },
  { title: '合理浮动比例', dataIndex: 'iProportion' },
  { title: '辅计量单位序号', dataIndex: 'iNumber' },
  // { title: '时间戳', dataIndex: 'pubufts'},
  // { title: '英文名称单数', dataIndex: 'cEnSingular'},
  // { title: '英文名称复数', dataIndex: 'cEnPlural'},
  { title: '对应存货编码', dataIndex: 'cUnitRefInvCode' }
  // {
  //   title: '创建时间',
  //   dataIndex: 'CreateTime',
  //   width: '10%',
  //   align: 'center',
  //   scopedSlots: { customRender: 'CreateTime' }
  // },
  // { title: '操作', dataIndex: 'action', align: 'left', scopedSlots: { customRender: 'action' } }
]

export default {
  props: {
    parentObj: Object
  },
  data() {
    return {
      columns,
      data: [],
      selectedRowKeys: [],
      pagination: {
        current: 1,
        pageSize: 25,
        showTotal: (total, range) => `总数:${total} 当前:${range[0]}-${range[1]}`,
        pageSizeOptions: ['25', '50', '100'], // 设置pageSize的可选值，页面可以通过下拉框进行选择
        showSizeChanger: true
      },
      filters: {},
      sorter: { field: 'Id', order: 'desc' },
      layout: {
        labelCol: { span: 5 },
        wrapperCol: { span: 18 }
      },
      visible: false,
      loading: false,
      entity: {},
      rules: {},
      title: ''
    }
  },
  methods: {
    handleTableChange(pagination, filters, sorter) {
      this.pagination = { ...pagination }
      this.filters = { ...filters }
      
      this.getDataList()
    },
    onSelectChange(selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
    close() {
      this.visible = false

      this.entity = {}
    },
    init() {
      this.visible = true
      this.entity = {}
      this.$nextTick(() => {
        this.$refs['form'].clearValidate()
      })
    },
    openForm(id, title) {
      this.init()
      this.title = title
      //console.log(id)
      if (id) {
        this.loading = true
        this.$http.post('/ProductManagement/ComputationGroup/GetTheData', { id: id }).then(resJson => {
          this.loading = false

          this.entity = resJson.Data
        })
        this.$http
          .post('/ProductManagement/ComputationUnit/GetDataList', {
            PageIndex: this.pagination.current,
            PageRows: this.pagination.pageSize,
            SortField: this.sorter.field || 'Id',
            SortType: this.sorter.order,
            Search: { Condition: 'cGroupCode', Keyword: id },
            ...this.filters
          })
          .then(resJson => {
            this.loading = false
            this.data = resJson.Data
            if (this.data) {
              const pagination = { ...this.pagination }
              pagination.total = resJson.Total
              this.pagination = pagination
            }
          })
      }
    },
    handleSubmit() {
      this.$refs['form'].validate(valid => {
        if (!valid) {
          return
        }
        this.loading = true
        this.$http.post('/ProductManagement/ComputationGroup/SaveData', this.entity).then(resJson => {
          this.loading = false

          if (resJson.Success) {
            this.$message.success('操作成功!')
            this.visible = false

            this.parentObj.getDataList()
          } else {
            this.$message.error(resJson.Msg)
          }
        })
      })
    }
  }
}
</script>
